package com.yogee.topseller.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.trello.rxlifecycle.LifecycleTransformer;
import com.umeng.analytics.MobclickAgent;
import com.yogee.core.base.HttpView;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.SharedPreferencesUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.HyqxActivity;
import com.yogee.topseller.activity.PayActivity;
import com.yogee.topseller.activity.login.model.bean.UserBean;
import com.yogee.topseller.activity.my.MyCommissionActivity;
import com.yogee.topseller.activity.my.PersonalDataActivity;
import com.yogee.topseller.activity.my.TakeCashActivity;
import com.yogee.topseller.bean.Hymodel;
import com.yogee.topseller.bean.ViewInformationBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.MyMenuLayout;
import com.yogee.topseller.layout.MyOrderLayout;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.Constants;
import com.yogee.topseller.utils.GlideUtils;
import com.yogee.topseller.utils.LogUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/20.
 */
public class MyFragment extends Fragment implements HttpView {

    @BindView(R.id.my_bg_iv)
    ImageView myBgIv;
    @BindView(R.id.my_photo_iv)
    ImageView myPhotoIv;
    @BindView(R.id.my_name_tv)
    TextView myNameTv;
    @BindView(R.id.my_order_layout)
    MyOrderLayout myOrderLayout;
    @BindView(R.id.my_commmoney_tv)
    TextView commoneyTv;
    @BindView(R.id.my_unablemoney_tv)
    TextView unablemoneyTv;
    Unbinder unbinder;
    @BindView(R.id.my_menu_layout)
    MyMenuLayout myMenuLayout;
    private String userid;

    @BindView(R.id.rl_baseinfo)
    RelativeLayout rl_baseinfo;
    @BindView(R.id.tv_hy_status)
    ImageView tv_hy_status;
    @BindView(R.id.tv_hy_go_charge)
    TextView tv_hy_go_charge;
    @BindView(R.id.rl_hy_info)
    RelativeLayout rl_hy_info;
    @BindView(R.id.tv_hy_time)
    TextView tv_hy_time;
    @BindView(R.id.tv_hy_go_charge1)
    TextView tv_hy_go_charge1;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_my, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取会员信息
     * @param userId
     */
    private void getHyInfo(String userId){
        HttpManager.getInstance().viewInformationDay(userid).subscribe(new BaseSubscriber(new HttpOnNextListener<Hymodel>() {
            @Override
            public void onNext(Hymodel o) {
                // 普通会员
                if(!o.getStauts().equals("1")){
                    rl_baseinfo.setBackgroundResource(R.drawable.myinfo_background);
                    tv_hy_status.setImageResource(R.drawable.pthy);
//                    tv_hy_status.setText("普通会员");
//                    tv_hy_status.setTextColor(getActivity().getResources().getColor(R.color.hint_color1));
                    tv_hy_go_charge.setVisibility(View.VISIBLE);
                    rl_hy_info.setVisibility(View.GONE);
                }else{
                    rl_baseinfo.setBackgroundResource(R.drawable.myinfo_background_huiyuan);
                    tv_hy_go_charge.setVisibility(View.GONE);
                    rl_hy_info.setVisibility(View.VISIBLE);
                    tv_hy_time.setText(o.getDay()+"天");
                    if(o.getVip().equals("年卡")){
//                        tv_hy_status.setText("专业版");
//                        tv_hy_status.setTextColor(getActivity().getResources().getColor(R.color.select_color));
                        tv_hy_status.setImageResource(R.drawable.nianka);
                    }else{
                        tv_hy_status.setImageResource(R.drawable.yueka);
//                        tv_hy_status.setText("个人版");
//                        tv_hy_status.setTextColor(getActivity().getResources().getColor(R.color.color4));
                    }
                }
            }
        }, this));



    }

    @OnClick({R.id.rl_hyms,R.id.tv_hy_go_charge1,R.id.tv_hy_go_charge,R.id.my_personal_rl, R.id.my_yj_tv, R.id.my_commission_tx_tv,R.id.my_title_more,R.id.ll_yj})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            // 会员权限
            case R.id.rl_hyms:
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), HyqxActivity.class).putExtra("url", Constants.HYQX));
                }
                break;
            case R.id.tv_hy_go_charge:
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), PayActivity.class));
                }
                break;
            case R.id.tv_hy_go_charge1:
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), PayActivity.class));
                }
                break;
            case R.id.my_personal_rl://个人资料
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), PersonalDataActivity.class));
                }
                break;
            case R.id.my_yj_tv://佣金
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), MyCommissionActivity.class));
                }
                break;
            case R.id.my_commission_tx_tv://提现按钮
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), TakeCashActivity.class));
                }
                break;
            case R.id.my_title_more:
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), PersonalDataActivity.class));
                }
                break;
            case R.id.ll_yj:
                if (AppUtil.isExamined(getActivity())) {
                    startActivity(new Intent(getActivity(), MyCommissionActivity.class));
                }
                break;
        }
    }

    private void viewInformation(String userid) {
        HttpManager.getInstance().viewInformation(userid).subscribe(new BaseSubscriber(new HttpOnNextListener<ViewInformationBean>() {
            @Override
            public void onNext(ViewInformationBean o) {
                saveUser(o);
                setViewData(o);
            }
        }, this));
    }

    private void saveUser(ViewInformationBean o) {
        String sex = AppUtil.getSex(getActivity());
        AppUtil.clearUserInfo(getActivity());
        UserBean userBean = new UserBean();
        userBean.setName(o.getName());
        userBean.setApprstats(o.getApprstats());
        userBean.setCommmoney(o.getCommmoney());
        userBean.setResult(o.getResult());
        userBean.setUnablemoney(o.getUnablemoney());
        userBean.setUserId(userid);
        userBean.setUserImg(o.getUserImg());
        userBean.setSex(sex);
        AppUtil.saveUserInfo(getActivity(), userBean);

        SharedPreferencesUtils.put(getActivity(),"money",o.getUnablemoney());
    }

    // 获取用户信息
    private void getUserInfo(){
        AppUtil.getUserInfo(getActivity());

    }

    private void setViewData(ViewInformationBean o) {
        GlideUtils.circleImage(getActivity(), o.getUserImg(), myPhotoIv);
        myNameTv.setText(o.getName());
        commoneyTv.setText(o.getCommmoney());
        unablemoneyTv.setText(o.getUnablemoney());
        myMenuLayout.setCounts(o.getCounts(),o.getKfPhone());
        myOrderLayout.setBean(o);
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void loadingFinished() {

    }

    @Override
    public void showMsg(String msg) {

    }

    @Override
    public <T> LifecycleTransformer<T> bindRecycler() {
        return null;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (AppUtil.getUserId(getActivity()) != null) {
            LogUtils.e("homeinit","44444");
            userid = AppUtil.getUserId(getActivity());
            viewInformation(userid);
            getHyInfo(userid);
        }
        MobclickAgent.onPageStart("我的"); //统计页面("MainScreen"为页面名称，可自定义)
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我的");
    }
}